package com.example.radiobutton;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class radiobtn extends AppCompatActivity {
    Button submit;
    RadioGroup options;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radiobtn);

        submit = (Button) findViewById(R.id.submitBtn);
        options = (RadioGroup)findViewById(R.id.rgroup);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0;i < options.getChildCount();i++)
                {
                    RadioButton option = (RadioButton)options.getChildAt(i);
                    if (option.isChecked())
                    {
                        if (option.getText().equals(getResources().getString(R.string.option_B))){
                            Toast.makeText(radiobtn.this,R.string.result_correct,Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            AlertDialog.Builder builder = new AlertDialog.Builder(radiobtn.this);
                            builder.setMessage(R.string.result_error);
                            builder.setPositiveButton(R.string.result_confirm,null).show();
                        }
                        break;
                    }
                }
            }
        });

    }
}